<template>
  <el-card class="form-container" shadow="never">
    <el-form :model="game" :rules="rules" ref="gameFrom" label-width="180px">
      <el-form-item label="手机名称：" prop="productName">
        <el-input v-model="game.productName"></el-input>
      </el-form-item>
      <el-form-item label="原神最高画质帧率， 单位：FPS">
        <el-input v-model="game.genshinFps"></el-input>
      </el-form-item>
      <el-form-item label="半小时原神整机平均功耗， 单位：W">
        <el-input v-model="game.genshinConsumption"></el-input>
      </el-form-item>
      <el-form-item label="和平精英流畅画质+90帧的帧率， 单位：FPS">
        <el-input v-model="game.hpFps"></el-input>
      </el-form-item>
      <el-form-item label="和平精英流畅画质+90帧的功耗， 单位：W">
        <el-input v-model="game.hpConsumption"></el-input>
      </el-form-item>
    
      <el-form-item>
        <el-button type="primary" @click="onSubmit('gameFrom')">提交</el-button>
        <el-button v-if="!isEdit" @click="resetForm('gameFrom')">重置</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>
<script>
  import {createGame, getGame, updateGame} from '@/api/game'
  import SingleUpload from '@/components/Upload/singleUpload'
  const defaultGame={
    productName: '',
    genshinFps: 0,
    genshinConsumption: 0,
    hpFps: 0,
    hpConsumption: 0
  };
  export default {
    name: 'GameDetail',
    components:{SingleUpload},
    props: {
      isEdit: {
        type: Boolean,
        default: false
      }
    },
    data() {
      return {
        game:Object.assign({}, defaultGame),
        rules: {
          productName: [
            {required: true, message: '请输入手机名称', trigger: 'blur'},
            {min: 2, max: 140, message: '长度在 2 到 140 个字符', trigger: 'blur'}
          ]
        }
      }
    },
    created() {
      if (this.isEdit) {
        getGame(this.$route.query.id).then(response => {
          this.game = response.data;
        });
      }else{
        this.game = Object.assign({},defaultGame);
      }
    },
    methods: {
      onSubmit(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.$confirm('是否提交数据', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {
              if (this.isEdit) {
                updateGame(this.$route.query.id, this.game).then(response => {
                  this.$refs[formName].resetFields();
                  this.$message({
                    message: '修改成功',
                    type: 'success',
                    duration:1000
                  });
                  this.$router.back();
                });
              } else {
                createGame(this.game).then(response => {
                  this.$refs[formName].resetFields();
                  this.game = Object.assign({},defaultGame);
                  this.$message({
                    message: '提交成功',
                    type: 'success',
                    duration:1000
                  });
                });
              }
            });

          } else {
            this.$message({
              message: '验证失败',
              type: 'error',
              duration:1000
            });
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
        this.game = Object.assign({},defaultGame);
      }
    }
  }
</script>
<style>
</style>


